from PyQt5.QtWidgets import QMainWindow
from threading import Thread

from PyQt5.uic.properties import QtCore

from frame.about_rc import Ui_aboutFrame
from frame.Ranking_rc import Ui_rankingRrame
from frame.login_rc import *
from game_main import start_game
from mapper.user_mapper import User_mapper
from mapper.log_mapper import Log_Mapper
from mediapipe_test import *

import globals

class LoginForme(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Frame()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.ui.exitButtom.clicked.connect(self.clickButtonCloseWindow)
        self.ui.startButtom.clicked.connect(self.show_game)
        self.ui.testButtom.clicked.connect(self.show_test)
        self.ui.adoutButtom.clicked.connect(self.show_about)
        self.ui.rankingButtom.clicked.connect(self.show_ranking)
        self.show()

    def clickButtonCloseWindow(self):
        self.close()

    def show_game(self):
        user_name = self.ui.usernameInput.toPlainText()
        if user_name != "":
            user_mapper:User_mapper = User_mapper("./db/data.db")
            log_mapper:Log_Mapper = Log_Mapper("./db/data.db")
            score = user_mapper.login_game(user_name)
            log_mapper.save_info_log(user_name,"Login")
            globals.set_user_name(user_name)
            # 写入最高分至临时文件
            with open('./record.ini', 'w') as file:
                file.write(str(score))
            self.clickButtonCloseWindow()
            start_game()

    def show_test(self):
        Thread(target=detect()).start()

    def show_about(self):
        self.form2 = QtWidgets.QWidget()
        self.ui2 = Ui_aboutFrame()
        self.ui2.setupUi(self.form2)
        self.form2.show()

    def show_ranking(self):
        self.form3 = QtWidgets.QWidget()
        self.ui3 = Ui_rankingRrame()
        self.ui3.setupUi(self.form3)
        self.form3.show()